package com.neuedu.security.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

@ApiModel("通用的返回结果")
public class Ret<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 成功
     */
    public static final int SUCCESS = HttpStatus.OK.value();

    /**
     * 失败
     */
    public static final int FAIL = HttpStatus.INTERNAL_SERVER_ERROR.value();

    @ApiModelProperty(value = "响应结果")
    private int code;

    @ApiModelProperty(value = "错误消息")
    private String msg;

    @ApiModelProperty(value = "返回数据")
    private T data;

    public static <T> Ret<T> ok() {
        return restResult(null, SUCCESS, "操作成功");
    }

    public static <T> Ret<T> ok(T data) {
        return restResult(data, SUCCESS, "操作成功");
    }

    public static <T> Ret<T> ok(T data, String msg) {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> Ret<T> fail() {
        return restResult(null, FAIL, "操作失败");
    }

    public static <T> Ret<T> fail(String msg) {
        return restResult(null, FAIL, msg);
    }

    public static <T> Ret<T> fail(T data) {
        return restResult(data, FAIL, "操作失败");
    }

    public static <T> Ret<T> fail(T data, String msg) {
        return restResult(data, FAIL, msg);
    }

    public static <T> Ret<T> fail(int code, String msg) {
        return restResult(null, code, msg);
    }

    public static <T> Ret<T> notFound(String msg) {
        return restResult(null, HttpStatus.NOT_FOUND.value(), msg);
    }


    /**
     * 没有认证 401
     *
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> Ret<T> unauthorized(String msg) {
        return restResult(null, HttpStatus.UNAUTHORIZED.value(), msg);
    }

    public static <T> Ret<T> unauthorized() {
        return restResult(null, HttpStatus.UNAUTHORIZED.value(), "登录过期");
    }

    /**
     * 授权失败的情况
     *
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> Ret<T> forbidden(String msg) {
        return restResult(null, HttpStatus.FORBIDDEN.value(), msg);
    }


    private static <T> Ret<T> restResult(T data, int code, String msg) {
        Ret<T> apiResult = new Ret<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> Boolean isError(Ret<T> ret) {
        return !isSuccess(ret);
    }

    public static <T> Boolean isSuccess(Ret<T> ret) {
        return Ret.SUCCESS == ret.getCode();
    }
}
